package bsk_dac.users;

import bsk_dac.MainDACFrame;
import bsk_dac.accesscontrol.TableAccess;
import bsk_dac.accesscontrol.AccessType;
import bsk_dac.accesscontrol.UserAccess;
import bsk_dac.database.DatabaseFacade;
import bsk_dac.tables.TablesManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class User {

    private String userName;
    private Map<String, TableAccess> userTableAccess;
    private UserAccess userAccess;

    public User(String userName) {
        this.userName = userName;
        userTableAccess = new HashMap<String, TableAccess>();
        userAccess = new UserAccess();
    }

    public TableAccess getTableAccess(String tableName) {
        return userTableAccess.get(tableName);
    }

    public UserAccess getUserAccess() {
        return userAccess;
    }

    public void setUserAccess(UserAccess userAccess) {
        this.userAccess = userAccess;
    }

    public String toString(){
        return userName;
    }

    public void loadAccessFromDatabase() {
        int user_id = 0;
        int table_id = 0;
        try {
            user_id = DatabaseFacade.getUserId(userName);

            ArrayList<String> tablesNames = TablesManager.getTableManager().getAvaiableTables();
            for (String tableName : tablesNames) {
                TableAccess tableAccess = new TableAccess();
                table_id = DatabaseFacade.getTableId(tableName);

                tableAccess.setPrivileges(AccessType.SELECT, DatabaseFacade.getTablePriveleges(user_id, table_id, "select"));
                tableAccess.setPrivileges(AccessType.INSERT, DatabaseFacade.getTablePriveleges(user_id, table_id, "insert"));
                tableAccess.setPrivileges(AccessType.DELETE, DatabaseFacade.getTablePriveleges(user_id, table_id, "delete"));
                tableAccess.setPrivileges(AccessType.UPDATE, DatabaseFacade.getTablePriveleges(user_id, table_id, "update"));

                tableAccess.setUserGivePermission(AccessType.SELECT, DatabaseFacade.getTableGivePriveleges(user_id, table_id, "select"));
                tableAccess.setUserGivePermission(AccessType.INSERT, DatabaseFacade.getTableGivePriveleges(user_id, table_id, "insert"));
                tableAccess.setUserGivePermission(AccessType.DELETE, DatabaseFacade.getTableGivePriveleges(user_id, table_id, "delete"));
                tableAccess.setUserGivePermission(AccessType.UPDATE, DatabaseFacade.getTableGivePriveleges(user_id, table_id, "update"));

                userTableAccess.put(tableName, tableAccess);

                userAccess = new UserAccess();
                boolean userGivePriv = DatabaseFacade.getUserGivePriv(userName);
                userAccess.setPrivileges(AccessType.GIVE_PRIV, userGivePriv);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public String getUserName() {
        return userName;
    }
}
